package org.carl.grpc.cloud.service.a.kit;

import com.google.common.collect.Lists;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * email carl.chen@bk.ru
 *
 * @Author: 陈青松
 * @Date: 2019/4/8 20:24
 */
public class JavaProtoKit {
    public static void java2Proto(List<Class<?>> classes) {
        StringBuilder b = new StringBuilder();
        for (Class<?> aClass : classes) {
            b.append(java2Proto(aClass));
        }
        System.out.println(b);
    }

    public static String java2Proto(Class<?> clazz) {
        StringBuilder b = new StringBuilder();
        List<Field> fields = ObjectKit.getFieldsByClass(clazz, Lists.newArrayList());
        b.append("message ").append(clazz.getSimpleName()).append("{").append("\n");
        int index = 1;
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            Class<?> type = field.getType();
            if ((!isJavaClass(type))&&!type.isEnum()) {
                //自定义类型
                String str=java2Proto(type);
                b.append(str);
            }
            b.append("    ");
            if (String.class == type) {
                b.append("string");
            } else if (Double.class == type) {
                b.append("double");
            } else if (Float.class == type) {
                b.append("float");
            }  else if (Date.class == type||Timestamp.class == type) {
                //日期类型转换为long类型
                b.append("int64");
            } else if (Long.class == type) {
                b.append("int64");
            } else if (Integer.class == type) {
                b.append("int32");
            } else if (Boolean.class == type) {
                b.append("bool");
            } else if (BigDecimal.class == type) {
                b.append("double");
            }else if(type.isEnum()){
                //枚举类型,需要枚举完整集合
                b.append("enum ");
                b.append(type.getName());
                b.append("{");
                //获取所有枚举类型
                Enum<?>[] ecs = (Enum[])type.getEnumConstants();
                for (Enum<?> ec:ecs) {
                    b.append(ec.name());
                    b.append("  = ");
                    b.append(ec.ordinal());
                    b.append(";");
                }
                b.append("}");
                b.append(type.getName());
            }else if (!isJavaClass(type)) {
                b.append(type.getName());
            }else {
                throw new RuntimeException(type + " 没处理");
            }
            b.append(" ").append(name).append(" ").append("=").append(" ").append(index).append(";");
            b.append("\n");
            index++;
        }
        b.append("}");
        b.append("\n");
        return b.toString();
    }

    /**
     * 判断一个类是JAVA类型还是用户定义类型
     *
     * @param clz
     * @return
     */
    public static boolean isJavaClass(Class<?> clz) {
        return clz != null && clz.getClassLoader() == null;
    }
}
